<script>
var goods_function_info;
var goods_mouting_genre;

function setOptions(data ,def) {
	var opt = '<option value=""></option>';
	$.each(data, function(index, value){
		opt += '<option value="'+ value.val +'" '+ (def==value.val ? 'selected':'') +'>'+ value.val +'</option>';
	});
	return opt;
}

function addTrLine(data) {
    var tr = '<tr><td><input type="checkbox" value=""  class="checkboxs"/></td>\
			 <td><input name="norms[]" class="norms" type="text" /></td>\
			 <td><input name="place_origin[]" class="place_origin" type="text"  /></td>\
			 <td><input name="dnumber[]" class="dnumber" type="text"  /></td>\
			 </tr>';
	$('#attrlist').append(tr);
	return tr;
}

$(function(){
	$.post('{{ urlfor "ProductsController.GetOptions"}}', {'fields':['goods_function_info', 'goods_mouting_genre']}, function(res){
		goods_mouting_genre = res.data.goods_mouting_genre;
		goods_function_info = res.data.goods_function_info;
		for(i=0;i<=1;i++){
			$('#addline').click();
		}
	});	
});

$('#addline').click(function(){
	addTrLine({});
	return false;	
});

$('#delline').click(function(){
	var checkboxs = $('.checkboxs:checked');
	if(checkboxs.length > 0) {
		checkboxs.each(function(index, element) {
           $(this).parent().parent().remove(); 
        });
	}
	return false;	
});

$('.selectAll').click(function(){
    if($(this).prop('checked') != false) {
        $('.checkboxs').prop('checked', true);
    } else {
        $('.checkboxs').prop('checked', false);
    }
});

$('#submitBtn').click(function(){
	var tr = $('#attrlist > tr');
	var yesLine = 0;
	var yesSubmit = true;
	var dataInfo = new Array();
	tr.each(function(index,value){
		var valueObj = $(value);
		var norms = valueObj.find('.norms').val();
		var place_origin = valueObj.find('.place_origin').val();
		var dnumber = valueObj.find('.dnumber').val();
		if(norms != '') {
			if(place_origin == '') {
				layer.alert('请填写产地',function(i){
					$('#attrlist > tr').eq(index).find('.place_origin').focus();
					layer.close(i);
				});
				yesSubmit = false;
				return;
			}
			if(dnumber == '') {
				layer.alert('请填写批准文号',function(i){
					$('#attrlist > tr').eq(index).find('.dnumber').focus();
					layer.close(i);
				});
				yesSubmit = false;
				return;
			}
			dataInfo.push({'Norms':norms,'PlaceOrigin':place_origin,'Dnumber':dnumber});
			yesLine++;
		}
	});
	if(yesSubmit && yesLine==0){
		layer.alert('请填入数据再保存');
		return;
	}
	$.post(location.href, {'dataInfo':JSON.stringify(dataInfo)}, function(res){
		if(res.code == 1) {
			layer.alert(res.msg);
			return false;	
		}
		parent.dialogReback();
		parent.layer.closeAll('iframe');
	}, 'json');
	
	return false;
});
</script>